/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package MyThread;

import MyLib.Comvar;
import java.net.DatagramPacket;
import java.net.InetAddress;
import java.nio.ByteBuffer;
import java.util.ArrayList;
import javax.swing.JTable;

/**
 *
 * @author QuangHau
 */
public class UpdateChunk extends Thread {

    private DatagramPacket packet;
    private JTable tbChunk;

    public UpdateChunk(DatagramPacket rcvpkt, JTable _tbChunk) {
        packet = rcvpkt;
        tbChunk = _tbChunk;
    }

    public synchronized void run() {
        Comvar.writeLog(1, UpdateChunk.class.getName(), "BEGIN UPDADE CHUNK THREAD " + Long.toString(this.getId()));
        if (!Comvar.stopFindChunk) {
            String stRcv;
            //get header
            byte[] header = new byte[14];
            System.arraycopy(packet.getData(), 0, header, 0, 14);

            //get lenght
            byte[] arr = {header[2], header[3]};
            ByteBuffer wrapped = ByteBuffer.wrap(arr); 
            short lenght = wrapped.getShort();

            //get data   
            byte[] data = new byte[lenght];
            System.arraycopy(packet.getData(), 14, data, 0, lenght);
            stRcv = new String(data, 0, lenght);

            //update chunk
            String[] arrStr = stRcv.split(";");
            System.out.println("update chunk " + stRcv);
             Comvar.writeLog(3, responFindChunk.class.getName(), "RECEIVE " + packet.getAddress().getHostAddress() + 
                                        " [Respon find chunk " + stRcv + "]");
            ArrayList<InetAddress> listIP = Comvar.listChunkIP.get(Integer.parseInt(arrStr[1]));
            int i;
            for (i = 0; i < listIP.size(); i++) {
                if (listIP.get(i).equals(packet.getAddress())) {
                    //da co trong danh sach
                    break;
                }
            }
            if (i == listIP.size()) {
                //ip moi
                listIP.add(packet.getAddress());
                for (int j = 0; j < tbChunk.getRowCount(); j++) {
                    if (tbChunk.getValueAt(j, 0).equals(arrStr[1])) {
                        tbChunk.setValueAt(listIP.size(), j, 1);
                    }
                }
            }

            for (i = 0; i < Comvar.listChunkIP.size(); i++) {
                if (Comvar.listChunkIP.get(i).size() == 0) {
                    //co mot chunk chua tim dc
                    break;
                }
                // System.out.println("chunk " + i + "  " + Comvar.listChunkIP[i].getIp().size());
            }

            if (i == Comvar.listChunkIP.size()) {
                //da du chunk
                Comvar.stopFindChunk = true;
                //ket thuc qua trinh tim chunk
                Comvar.threadFindChunk.release();
            }
        }
        Comvar.writeLog(1, UpdateChunk.class.getName(), "END UPDADE CHUNK THREAD " + Long.toString(this.getId()));
    }
}
